package day11;

import com.sun.corba.se.impl.orb.ParserTable;
import com.sun.jmx.snmp.SnmpUnknownAccContrModelException;

import java.util.Arrays;
import java.util.Comparator;

/**
 * @author yt1105
 * @version 1.0
 */
public class ArrayExercise {
    public static void main(String[] args) {
        Book[] books = new Book[4];
        books[0] = new Book("红楼梦", 120);
        books[1] = new Book("西游记", 220);
        books[2] = new Book("三国演义", 300);
        books[3] = new Book("水浒传", 129);
        Arrays.sort(books, new Comparator() {//按照书名的长度进行排序处理
            @Override
            public int compare(Object o1, Object o2) {
                Book b1 = (Book) o1;
                Book b2 = (Book) o2;
                return b1.getName().length() - b2.getName().length();//这个是按照书名的长度进行排序的话 从小到大
            }
        });
//        Arrays.sort(books, new Comparator() {
//            @Override
//            public int compare(Object o1, Object o2) {
//                Book b1 = (Book) o1;
//                Book b2 = (Book) o2;
//                double price = b1.getPrice() - b2.getPrice();
//                if (price > 0) {
//                    return -1;
//                } else if (price < 0) {
//                    return 1;
//                } else {
//                    return 0;
//                }
//            }
//        });

        System.out.println(Arrays.toString(books));
    }

}


class Book {
    private String name;
    private double price;

    public Book(String name, double price) {
        this.name = name;
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", price=" + price +
                '}';
    }
}
